<div class="red" style="line-height: 34px;">
    【操作提示】点击标题可查看详情
</div>
<table class="am-table am-table-bordered am-table-striped zjjc">
    <thead>
        <tr>
            <th class="t">平台资金</th>
            <th class="charge"><a href="#">资金流入 <img src="../public/all/images/important.png" height="20px"/></a></th>
            <th>资金流出</th>
            <th>差额</th>
            <th>系统余额</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
<div id="ptzj" style="margin: 10px 0px 10px 10px; width: 800px; height: 300px;">
</div>

<table class="am-table am-table-bordered am-table-striped zdtj">
    <thead>
        <tr>
            <th class="t">盈亏报表</th>
            <th>纯利润</th>
            <th>下注总额</th>
            <th>返水总额</th>
            <th>撤单总额</th>
            <th>注单盈利</th>
            <th>奖励总额</th>
            <th>惩罚扣款</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
<div id="ykbb" style="margin: 10px 0px 10px 10px; width: 800px; height: 300px;">
</div>

<table class="am-table am-table-bordered am-table-striped yh">
    <thead>
        <tr>
            <th class="t">用户数据</th>
            <th>累积用户</th>
            <th>新增用户</th>
            <th>充值用户</th>
            <th>新增充值用户</th>
            <th>登录用户</th>
            <th>投注用户</th>
        </tr>
    </thead>
    <tbody>
    </tbody>
</table>
<div id="yhsj" style="margin: 10px 0px 10px 10px; width: 800px; height: 300px;">
</div>

<style type="text/css">
    .am-table th.t {
        font-size: 14px;
        font-weight: bold;
        color: blue;
    }
</style>

<script>
    // 近7日的数据
    var datas = [];
    var end = new Date().getTime();
    var start = end - 6 * 24 * 3600 * 1000;
    CMD.bet_sum_gquery('#SUM#', new Date(start).Format("yyyy-MM-dd"), 
        new Date(end).Format("yyyy-MM-dd"), undefined, function(r) {
        r.sort(function(b, a) {
            return a.date.localeCompare(b.date); 
        });
        datas = r;

        CMD.stat_sum_gdquery(new Date(start).Format("yyyy-MM-dd"), new Date(end).Format("yyyy-MM-dd"), function(r2) {
            // 填入datas
            for (var i in r2) {
                var d = r2[i];
                for (var j in datas) {
                    if (datas[j].date != d.date) continue;

                    // 合并
                    for (var k in d) {
                        if (k == 'deposit' && typeof(d[k]) == 'string') datas[j][k] = JSON.parse(d[k]);
                        else
                            datas[j][k] = d[k];
                    }
                }
            }

            fillMoneyInOut();
            winLose();
            userDatas();
        });
    });

    // 资金进入
    function fillMoneyInOut() {
        var table = $(".zjjc tbody");
        table.empty();
        for (var i = 0; i < datas.length; i++) {
            var d = datas[i];
            tr = $("<tr></tr>");
            if (i == 0) tr.append("<td>今天</td>");
            else if (i == 1) tr.append("<td>昨天</td>");
            else if (i == 2) tr.append("<td>前天</td>");
            else tr.append("<td>" + (i + 1) + "天前</td>");
            
            if (d.deposit)
                tr.append("<td>" + ((d.deposit.total || 0)/100).toFixed(2) + "</td>");
            else
                tr.append("<td>0.00</td>");
            tr.append("<td>" + ((d.withdraw || 0)/100).toFixed(2) + "</td>");

            var v = d.deposit ? (d.deposit.total || 0) : 0;
            if (d.withdraw) v -= d.withdraw;

            tr.append("<td>" + (v/100).toFixed(2) + "</td>");
            tr.append("<td>" + ((d.balance || 0)/100).toFixed(2) + "</td>");

            table.append(tr);
        }

        // 图表
        var series = [];
        var f = function(title, cb) {
            var data = {
                name: title,
                type: 'line',
                stack: '总额',
                data: [],
            }
            for (var i = datas.length - 1; i >= 0; i--) {
                data.data.push(cb(datas[i]));
            }
            series.push(data);
        }
        f('资金流入', function(d) {
            return d.deposit ? d.deposit.total/100 : 0;
        });
        f('资金流出', function(d) {
            return (d.withdraw || 0)/100;
        });
        f('差额', function(d) {
            var v = d.deposit ? d.deposit.total : 0;
            if (d.withdraw) v -= d.withdraw;
            return v/100;
        });
        f('系统余额', function(d) {
            return (d.balance || 0)/100;
        });

        var c = echarts.init($("#ptzj")[0]);
        var option = {
            title: {
                text: "平台资金",
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: ['资金流入', '资金流出', '差额', '系统余额'],
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: ['6天前', '5天前', '4天前', '3天前', '前天', '昨天', '今天',],
            },
            yAxis: {
                type: 'value',
            },
            series: series,
        };
        c.setOption(option)
    }

    // 盈亏总览
    function winLose() {
        var table = $(".zdtj tbody");
        table.empty();
        for (var i = 0; i < datas.length; i++) {
            var d = datas[i];
            tr = $("<tr></tr>");
            if (i == 0) tr.append("<td>今天</td>");
            else if (i == 1) tr.append("<td>昨天</td>");
            else if (i == 2) tr.append("<td>前天</td>");
            else tr.append("<td>" + (i + 1) + "天前</td>");

            var v = 0;
            if (d.profit) v = d.profit;
            if (d.penalty) v += d.penalty;
            if (d.back) v -= d.back;
            if (d.reward) v -= d.reward;
            tr.append("<td>" + (v/100).toFixed(2) + "</td>");
            tr.append("<td>" + (d.amount/100).toFixed(2) + "</td>");
            tr.append("<td>" + (d.back/100).toFixed(2) + "</td>");
            tr.append("<td>" + ((d.cancel || 0)/100).toFixed(2) + "</td>");
            tr.append("<td>" + ((d.profit || 0)/100).toFixed(2) + "</td>");
            tr.append("<td>" + ((d.reward || 0)/100).toFixed(2) + "</td>");
            tr.append("<td>" + ((d.penalty || 0)/100).toFixed(2) + "</td>");

            table.append(tr);
        }

        // 图表
        var series = [];
        var f = function(title, cb) {
            var data = {
                name: title,
                type: 'line',
                stack: '总额',
                data: [],
            }
            for (var i = datas.length - 1; i >= 0; i--) {
                data.data.push(cb(datas[i]));
            }
            series.push(data);
        }
        f('纯利润', function(d) {
            var v = 0;
            if (d.profit) v = d.profit;
            if (d.penalty) v += d.penalty;
            if (d.back) v -= d.back;
            if (d.reward) v -= d.reward;
            return (v/100).toFixed(2);
        });
        f('下注', function(d) {
            return (d.amount/100).toFixed(2);
        });
        f('返水', function(d) {
            return (d.back/100).toFixed(2);
        });
        f('撤单', function(d) {
            return ((d.cancel || 0)/100).toFixed(2);
        });
        f('注单盈利', function(d) {
            return (d.profit/100).toFixed(2);
        });
        f('奖励', function(d) {
            return ((d.reward || 0)/100).toFixed(2);
        });
        f('扣款', function(d) {
            return ((d.penalty || 0)/100).toFixed(2);
        });

        var c = echarts.init($("#ykbb")[0]);
        var option = {
            title: {
                text: "盈亏报表",
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: ['纯利润', '下注', '返水', '撤单', '注单盈利', '奖励', '扣款'],
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: ['6天前', '5天前', '4天前', '3天前', '前天', '昨天', '今天',],
            },
            yAxis: {
                type: 'value',
            },
            series: series,
        };
        c.setOption(option)
    }

    // 用户数据
    function userDatas() {
        var table = $(".yh tbody");
        table.empty();
        for (var i = 0; i < datas.length; i++) {
            var d = datas[i];
            tr = $("<tr></tr>");
            if (i == 0) tr.append("<td>今天</td>");
            else if (i == 1) tr.append("<td>昨天</td>");
            else if (i == 2) tr.append("<td>前天</td>");
            else tr.append("<td>" + (i + 1) + "天前</td>");

            tr.append("<td>" + (d.userAll || 0) + "</td>");
            tr.append("<td>" + (d.userNew || 0) + "</td>");
            tr.append("<td>" + (d.userDeposit || 0) + "</td>");
            tr.append("<td>" + (d.userDepositFirst || 0) + "</td>");
            tr.append("<td>" + (d.userSignIn || 0) + "</td>");
            tr.append("<td>" + (d.userBet || 0) + "</td>");

            table.append(tr);
        }

        // 图表
        var series = [];
        var f = function(title, cb) {
            var data = {
                name: title,
                type: 'line',
                stack: '总额',
                data: [],
            }
            for (var i = datas.length - 1; i >= 0; i--) {
                data.data.push(cb(datas[i]));
            }
            series.push(data);
        }
        f('累积用户', function(d) {
            return d.userAll || 0;
        });
        f('新增用户', function(d) {
            return d.userNew || 0;
        });
        f('充值用户', function(d) {
            return d.userDeposit || 0;
        });
        f('新增充值用户', function(d) {
            return d.userDepositFirst || 0;
        });
        f('登录用户', function(d) {
            return d.userSignIn || 0;
        });
        f('投注用户', function(d) {
            return d.userBet || 0;
        });

        var c = echarts.init($("#yhsj")[0]);
        var option = {
            title: {
                text: "用户数据",
            },
            tooltip: {
                trigger: 'axis'
            },
            legend: {
                data: ['累积用户', '新增用户', '充值用户', '新增充值用户', '登录用户', '投注用户'],
            },
            xAxis: {
                type: 'category',
                boundaryGap: false,
                data: ['6天前', '5天前', '4天前', '3天前', '前天', '昨天', '今天',],
            },
            yAxis: {
                type: 'value',
            },
            series: series,
        };
        c.setOption(option)
    }

    $(".charge a").click(function() {
        Util.addPopUp("charge_stat", "charge_stat", 700);
    })
</script>